package com.linxianqin.springbootVue.controller.monitor;

import com.linxianqin.springbootVue.domain.constant.CacheStants;
import com.linxianqin.springbootVue.domain.constant.Constants;
import com.linxianqin.springbootVue.domain.entity.SysUserOnline;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.model.LoginUser;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.CacheService;
import com.linxianqin.springbootVue.service.SysUserOnlineService;
import com.linxianqin.springbootVue.utils.text.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author lxq
 * @version 1.0
 * @description: 在线用户监控
 * @date 2020/12/28 17:00
 */
@Slf4j
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {

    private final SysUserOnlineService userOnlineService;
    private final CacheService cacheService;
    public SysUserOnlineController(CacheService cacheService,SysUserOnlineService userOnlineService){
        this.cacheService=cacheService;
        this.userOnlineService=userOnlineService;
    }

    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/list")
    public TableDataInfo list(String ipaddr, String userName)
    {
//        log.info("获取的ipaddr：{}",ipaddr);
//        log.info("获取的userName：{}",userName);
        List<String> keys = cacheService.keys(CacheStants.USER_KEY);
//        log.info("获取的keys集合：{}",keys);
        List<SysUserOnline> userOnlineList = new ArrayList<>();
        for (String key : keys)
        {
            Object o = cacheService.get(CacheStants.USER_KEY, key);
            if (o==null){
                continue;
            }
            LoginUser user = (LoginUser) o;
//            log.info("获取的用户信息：{}",user);
//            log.info("获取的 user.getIpaddr()：{}",user.getIpaddr());
//            log.info("获取的 user.getUsername()：{}", user.getUsername());
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
            {
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByInfo(ipaddr, userName, user));
                }
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
                if (StringUtils.equals(ipaddr, user.getIpaddr()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByIpaddr(ipaddr, user));
                }
            }
            else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getUser()))
            {
                if (StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(userOnlineService.selectOnlineByUserName(userName, user));
                }
            }
            else
            {
                userOnlineList.add(userOnlineService.loginUserToUserOnline(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        return getDataTable(userOnlineList);
    }

    /**
     * 强退用户
     */
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @SysLog(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public AjaxResult forceLogout(@PathVariable String tokenId)
    {
        cacheService.remove(CacheStants.USER_KEY, Constants.LOGIN_TOKEN_KEY + tokenId);
        return AjaxResult.success();
    }
}
